<?php
require_once 'BaseController.php';

/**
 * 【PC版】建物問い合わせ コントローラ
 */
class Renew_ArchentryController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $this->doAction();
    }

    /**
     * メインアクション(post)
     */
    public function postAction()
    {
        $this->doAction();
    }

    /**
     * 処理実行
     */
    private function doAction()
    {
        $params = $this->getParameters();

        $code = $params['TatemonoCd'];
        $step = $params['Step'];
        $ShopCd = $params['ShopCd0'];
        if (empty($ShopCd) && isset($params['ShopCd']))
        {
            $ShopCd = $params['ShopCd'];
        }

        // 20141208 S_Add
        // 完了Stepの場合、データ取得は行わない
        if ($step != 'complete')
        {
            $this->_data['params'] = $params;

            $this->makeTemplateParams($code);
            $this->makeTemplateParams2($params, $ShopCd);

            $p_campaigns = $this->collectCampaignFromParams($params);
            $this->_data['p_campaigns'] = $p_campaigns;

            $model_cam = new Renew_Model_BukkenCampaign();
            $temp = $model_cam->getNormalCampaigns(); // キャンペーンデータ取得
        }
        // 20141208 E_Add

        if (empty($step))
        {
            $this->_data['CAMPAIGNS'] = $temp;
            $this->displayBody2('entry');
        }
        else if ($step == 1)
        {

            if ($this->checkParams($params))
            {
                // 入力にエラーがある場合
                $campaigns = array();
                foreach ($temp as $k => $cam)
                {
                    if (in_array($cam['M94_CAMPAIGN_CD'], $p_campaigns))
                    {
                        $cam['name'] = 'campaign' . $k;
                        $cam['checked'] = 1;
                    }
                    $campaigns[] = $cam;
                }
                $this->_data['CAMPAIGNS'] = $campaigns;
                // エラー画面へ遷移
                $this->displayBody2('error');
            }
            else
            {
                // 入力にエラーがない場合
                $campaigns = array();
                foreach ($temp as $k => $cam)
                {
                    if (in_array($cam['M94_CAMPAIGN_CD'], $p_campaigns))
                    {
                        $cam['name'] = 'campaign' . $k;
                        $campaigns[] = $cam;
                    }
                }
                $this->_data['CAMPAIGNS'] = $campaigns;
                // 確認画面へ遷移
                $this->displayBody2('confirm');
            }
        }
        else if ($step == 2)
        {

            $cparams = array();
            $cparams['Tel'] = $params['Tel'];
            $cparams['Mail'] = $params['Mail'];
            $cparams['vintGenruFlg'] = 0;
            $cparams['NameSei'] = $params['KanjiSei'];
            $cparams['NameMei'] = $params['KanjiMei'];
            // 20141130 S_Add
            $cparams['NameSeiKana'] = $params['KanaSei'];
            $cparams['NameMeiKana'] = $params['KanaMei'];
            // 20141130 E_Add

            // $list = $this->extractParams ( $params, 'TatemonoCd' );
            // $cparams ['TatemonoCd'] = join ( ',', $list );

            $cparams['TatemonoCd'] = $params['TatemonoCd'];
            $cparams['ShopCd'] = $params['ShopCd'];

            // 20141213 S_Add
            $cparams['OrgTatemonoCd'] = $params['OrgTatemonoCd'];
            $cparams['TatemonoNameKoukai'] = $params['TatemonoNameKoukai'];
            $cparams['TatemonoName'] = $params['TatemonoName'];
            $cparams['TatemonoJyuushoText'] = $params['TatemonoJyuushoText'];
            $cparams['EnsenName'] = $params['EnsenName'];
            $cparams['EkiName'] = $params['EkiName'];
            $cparams['BusGaisha'] = $params['BusGaisha'];
            $cparams['BusStop'] = $params['BusStop'];
            $cparams['BusJyousha'] = $params['BusJyousha'];
            $cparams['Toho01'] = $params['Toho01'];
            $cparams['TatemonoKouzou'] = $params['TatemonoKouzou'];
            $cparams['Kanseinen'] = $params['Kanseinen'];
            $cparams['Kanseitsuki'] = $params['Kanseitsuki'];
            $cparams['IS_FREE_CALL'] = $params['IS_FREE_CALL'];
            $cparams['CONVERT_CONNECTION_NO'] = $params['CONVERT_CONNECTION_NO']; // 20150128 Add
            $cparams['ShopName'] = $params['ShopName'];
            $cparams['ShopTel'] = $params['ShopTel'];
            $cparams['ShopMail'] = $params['ShopMail'];
            // 20141213 E_Add

            $list = $this->extractParams($params, 'check');
            if (array_key_exists('check1', $params)) $cparams['Inquiry1'] = 1;
            if (array_key_exists('check2', $params)) $cparams['Inquiry2'] = 1;
            if (array_key_exists('check3', $params)) $cparams['Inquiry3'] = 1;
            if (array_key_exists('check4', $params)) $cparams['Inquiry4'] = 1;

            $cparams['Comment'] = $params['shosai'];

            $list = $this->extractParams($params, 'campaign');
            $cparams['CampaignAgechau'] = join(';', $list);
            $cparams['TimeStr'] = date("Y/m/d H:i:s");

            $this->callInquiryApi('apiarticle', $cparams);

            $this->my_secure_redirect("inquiry/archentry/-/complete/?ShopCd={$params ['ShopCd']}");
        }
        else if ($step == 'complete')
        {
            // 検討リスト表示
            $this->loadData();
            $this->displayBody2('complete');
        }
    }

    /**
     * テンプレート用パラメータ作成
     *
     * @param unknown $code 建物コード
     */
    private function makeTemplateParams($code)
    {
        $model = new Rstdtl_Model_Articledtl();
        $model_conv = new Rstobi_Model_Converte();
        $model_shop = new Renew_Model_BukkenTenpo();

        $config = array();
        $params = array();

        $params['ID'] = $code;
        $params['buken_static'] = '1';
        $params['TatemonoCd'] = $code;

        $data = $model->getArticleDetail($code);
        $model_conv->dataConverteForBuken($data, $params, false, false);
        $model->convertData($data, $params);

        // OEM判定
        $OemFlg = ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));

        foreach ($data as $d)
        {
            $tatemono = $d['T03_DAIHYO_TATEMONO_CD'];
            if ($prev != $tatemono)
            {
                $c++;
                $items[] = array();
            }
            $d['MAX_CHINRYO_STR'] = (intval($d['MAX_CHINRYO']) / 10000);
            $d['MIN_CHINRYO_STR'] = (intval($d['MIN_CHINRYO']) / 10000);

            // 物件画像取得
            // $d ['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain ( $d, $OemFlg );
            // $d ['FULLGAZOU_ICON'] = Apm_Bukkenitems::GetFullGazouIcon ( $d ['BUKEN_IMAGE'] );

            // キャンペーン取得
            $heyaCd = $d['T03_HEYA_CD'];
            $ret = $model->getCamp($heyaCd);
            if (!empty($ret))
            {
                $d['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                $d['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                $d['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
            }

            $items[$c][] = $d;
            $prev = $tatemono;
        }

        // 都道府県
        $data[0]["T03_PREF_NAME"] = Apm_Config_Search::$TODOUFUKEN_NAME_CODE_LIST[$data[0]["T03_TODOUFUKEN_CD"]];
        $data[0]["T03_PREF_NAME_JP"] = Apm_Config_Shop::$PREF_LIST[$data[0]["T03_TODOUFUKEN_CD"]];

        $pref = $data[0]["T03_PREF_NAME_JP"];
        $city = $data[0]["T03_SHIKUCHOUSON_NAME_JP"];
        $address = $data[0]["T03_JYUUSHO_TEXT"];
        $data[0]["T03_CHOU_MEI"] = str_replace($city, "", str_replace($pref, "", $address));
        // 都道府県

        $tenpo1 = $model_shop->getTatemonoTenpoList($code);
        if (count($tenpo1) == 0)
        {
            $tenpo1 = $model_shop->getTenpoListTatemono($code);
        }
        $data[0]['tenpo1'] = $tenpo1;

        // CALLNOTE
        // 20150128 S_Comment
        // foreach ( $data [0] ['tenpo1'] as $i => $tenpo ) {
        // $tempData = Apm_ShopUtil::getCallingData ( $tenpo, Apm_Config_Shop::FREE_CONNECTION_NO_6
        // );
        // $data [0] ['tenpo1'] [$i] ['IS_FREE_CALL'] = $tempData ['IS_FREE_CALL'];
        // if ($tempData ['IS_FREE_CALL']) {
        // $data [0] ['tenpo1'] [$i] ['FREE_CALLING'] = $tempData ['TEL_01'];
        // $data [0] ['tenpo1'] [$i] ['CONVERT_CONNECTION_NO'] =
        // Apm_Bukkenitems::GetBukkenWebCallingNo ( $tenpo, Apm_Config_Rstdtl::CAMPAIGN_PC );
        // }
        // }
        // 20150128 E_Comment

        // ★★★周辺物件探す
        // $shuuhenBukken = $model->getShuuhenBukken ( $data [0] );
        // foreach ( $shuuhenBukken as $d ) {
        // $d ['T03_IMAGE_DATA'] [Apm_Config_Search::IMG_TYPE_GAIKAN] =
        // Apm_Bukkenitems::GetImageData ( $d, Apm_Config_Search::IMG_TYPE_GAIKAN, $OemFlg ); //
        // 画像データ
        // }
        // $this->_data ['shuuhen'] = $shuuhenBukken;
        // ★★★周辺物件探す

        $this->_data['buken'] = $data[0];
        $this->_data['bukenlist'] = $data;
        $this->_footer_data['ftBuken'] = $data[0];
    }

    /**
     * テンプレート用パラメータ作成(店舗情報を取得)
     *
     * @param unknown $params リクエストパラメータ
     * @param unknown $ShopCd 店舗コード
     */
    private function makeTemplateParams2($params, $ShopCd)
    {
        $model = new Shop_Model_Detail();

        $params = array();
        $params['ShopCd'] = $ShopCd;

        $data = $model->getList($params);

        $apalogurl = $this->_config['apm']['apl']['apalog']['baseurl'];
        $tenpoimageurl = $this->_config['apm']['apl']['imagesvurl'] . 'fs/tenpoimg/';
        $data = $model->convertData($data[0], $apalogurl, $tenpoimageurl);

        // 20150128 S_Add
        $callData = Apm_ShopUtil::getCallingData($data, Apm_Config_Shop::FREE_CONNECTION_NO_1);
        $data['CONNECTION_NO'] = $callData['CONNECTION_NO'];
        // 20150128 E_Add

        $this->_data['shop'] = $data;
    }

    /**
     * 入力チェック
     *
     * @param unknown $params リクエストパラメータ
     * @return number 0:OK,1:NG
     */
    private function checkParams($params)
    {
        $error = 0;

        // 姓・名の全角文字チェック
        $key = array();
        $key[] = 'kanji_sei';
        $key[] = 'kanji_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkaku($params[$k]))
            {
                $this->_data['name_error'] = 1;
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        // セイ・メイの全角カナ文字チェック
        $key = array();
        $key[] = 'kana_sei';
        $key[] = 'kana_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkakuKana($params[$k]))
            {
                $this->_data['name_error'] = "error";
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        // メールアドレス・電話番号チェック
        $params['mail'] = trim($params['mail']);
        $params['tel'] = trim($params['tel']);
        // if (empty ( $params ['mail'] ) && empty ( $params ['tel'] )) {
        // // メールアドレス・電話番号の両方が未入力の場合
        // $this->_data ['renraku_empty_error'] = "error";
        // $this->_data ['renraku_error'] = "error";
        // $error = 1;
        // } else if (! empty ( $params ['mail'] ) && $this->checkMail ( $params ['mail'] )) {
        // // メールアドレスが入力されているかつ、入力形式が正しくない場合
        // $this->_data ['mail_error'] = "error";
        // $this->_data ['renraku_error'] = "error";
        // $error = 1;
        // } else if (! empty ( $params ['tel'] ) && ! preg_match ( "/^[0-9]+$/u", $params ['tel']
        // )) {
        // // 電話番号が入力されているかつ、入力形式が正しくない場合
        // $this->_data ['tel_error'] = "error";
        // $this->_data ['renraku_error'] = "error";
        // $error = 1;
        // }

        if (empty($params['mail']) && empty($params['tel']))
        {
            // メールアドレス・電話番号の両方が未入力の場合
            $this->_data['renraku_empty_error'] = "error";
            $this->_data['renraku_error'] = "error";
            $error = 1;
        }
        else
        {
            if (!empty($params['mail']) && $this->checkMail($params['mail']))
            {
                // メールアドレスが入力されているかつ、入力形式が正しくない場合
                $this->_data['mail_error'] = "error";
                $this->_data['renraku_error'] = "error";
                $error = 1;
            }
            if (!empty($params['tel']) && !preg_match("/^[0-9]+$/u", $params['tel']))
            {
                // 電話番号が入力されているかつ、入力形式が正しくない場合
                $this->_data['tel_error'] = "error";
                $this->_data['renraku_error'] = "error";
                $error = 1;
            }
        }

        // お問い合わせ内容チェック
        $checks = 0;
        for ($i = 1; $i <= 4; $i++)
        {
            // チェックボックス４つ分をループ
            if ($params['check' . $i] == 'on')
            {
                // チェックボックスonがあれば、+1
                $checks++;
            }
        }
        if ($checks == 0)
        {
            // チェックボックスがすべてoffの場合
            $this->_data['checks_error'] = "error";
            $error = 1;
        }
        if (strlen($params['shosai']) > 500)
        {
            // 自由記載欄の文字数が500文字以上の場合
            $this->_data['shosai_error'] = "error";
            $error = 1;
        }

        return $error;
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $params = $this->getParameters();

        $subTitle = '';

        if ($params["Step"] == 1)
        {
            $subTitle = '入力内容の確認 ';
        }
        else if ($params["Step"] == 'complete')
        {
            $subTitle = '送信完了 ';
        }

        $this->_meta_title = '建物のお問い合わせ ' . $subTitle . '【アパマンショップ】';
    }

}
?>
